<?php

class Partner_model extends CI_Model {
	

    function __construct()
    {
        parent::__construct();
    }
    
	
	function get_single_data($select,$table,$fid,$id)
	{
		$this->db->select($select);
		$this->db->from($table);
		$this->db->where($fid,$id);
		$data = $this->db->get();
		
		if($data->num_rows() > 0)
		{
			foreach($data->result() AS $hasil)
			{
				return $hasil->$select;
			}
		}
		
		return FALSE;
	}
	
	function get_kategori_usaha()
	{
		$query = $this->db->get('dn_category');
		if($query->num_rows() > 0)
		{
			return $query->result();
		}
		
		return FALSE;
	}
	
	function cek_status_usaha($uid=0)
	{
		$this->db->where('uid', $uid);
		$this->db->where('status', 0);
		
		$hasil = $this->db->get('dn_company');
		if($hasil->num_rows() > 0)
		{
			return $hasil->result();
		}
		
		return FALSE;
	}
	
	
	function cek_pemilik_usaha($idp, $uid)
	{
		$this->db->select('idperusahaan');
		$this->db->from('dn_company');
		$this->db->where('idperusahaan', $idp);
		$this->db->where('uid', $uid);
		
		$query = $this->db->get();
		if($query->num_rows() > 0)
		{
			return TRUE;
		}
		
		return FALSE;
	}
	
	function get_daftar_perusahaan($uid)
	{
		$this->db->select('idperusahaan,nama');
		$this->db->where('uid',$uid);
		$query = $this->db->get('dn_company');
		if($query->num_rows() > 0){
			return $query->result();
		}
		return FALSE;
	}
	
	function get_perusahaan($uid)
	{
		
		$this->db->where('uid', $uid);
		$this->db->order_by('idperusahaan','desc');
		$query = $this->db->get('dn_company');
		
		if($query->num_rows() > 0){
			return $query->result();
		}
		return FALSE;
	}
	
	function get_data_perusahaan($idp, $uid)
	{
		
		$this->db->where('idperusahaan',$idp);
		$this->db->where('uid',$uid);
		
		$query = $this->db->get('dn_company');	
		if($query->num_rows() > 0){
			return $query->result();
		}
		return FALSE;
	}
	
	function get_batas_perusahaan($uid)
	{
		$this->db->where('uid',$uid);
		
		$query = $this->db->get('dn_company');
		if($query->num_rows() < 10)
		{
			return TRUE;
		} 
		return FALSE;
	}
	
	function set_perusahaan($data)
	{
		$this->db->insert('dn_company',$data);
		if($this->db->affected_rows() > 0)
		{
			return TRUE;
		}
		return FALSE;
	}
	
	function update_perusahaan($data, $idp, $uid)
	{
		$this->db->where('idperusahaan',$idp);
		$this->db->where('uid',$uid);
		$this->db->update('dn_company',$data);
		if($this->db->affected_rows() > 0)
		{
			return TRUE;
		}
		return FALSE;
	}
	
	function del_perusahaan($idc, $uid)
	{
		$this->db->where('idperusahaan', $idc);
		$this->db->where('uid', $uid);
		$this->db->delete('dn_company');
		
		if($this->db->affected_rows() > 0)
		{
			return TRUE;
		}
		
		return FALSE;
	}
	
	function get_daftar_produk($uid, $awal)
	{
		$this->db->select('p.idproduk, p.nama');
		$this->db->from('dn_product AS p');
		$this->db->join('dn_company AS c','c.idperusahaan=p.idperusahaan');
		$this->db->where('c.uid',$uid);
		$this->db->order_by('p.idproduk','desc');
		$this->db->limit(20, $awal);
		$query = $this->db->get();
		
		if($query->num_rows() > 0)
		{
			return $query->result();
		}
		return FALSE;
	}
	
	function hitung_produk($uid)
	{
		$this->db->select('p.idproduk');
		$this->db->from('dn_product AS p');
		$this->db->join('dn_company AS c','c.idperusahaan=p.idperusahaan');
		$this->db->where('c.uid', $uid);
		
		$hitung = $this->db->get();
		return $hitung->num_rows();
	}
	
	function get_data_produk($pid, $uid)
	{
		$this->db->select('p.idproduk AS idproduk, p.idperusahaan AS idperusahaan, p.nama AS nama, p.deskripsi AS deskripsi, p.gambar AS gambar')->from('dn_product AS p')->join('dn_company AS c','p.idperusahaan=c.idperusahaan');
		$this->db->where('p.idproduk', $pid);
		$this->db->where('c.uid', $uid);
		
		$query = $this->db->get();	
		if($query->num_rows() > 0){
			return $query->result();
		}
		return FALSE;
	}
	
	function cek_pemilik_produk($idp, $uid)
	{
		$this->db->select('p.idproduk');
		$this->db->from('dn_product AS p');
		$this->db->join('dn_company AS q', 'p.idperusahaan=q.idperusahaan');
		$this->db->where('p.idproduk', $pid);
		$this->db->where('q.uid', $uid);
		
		$query = $this->db->get();
		if($query->num_rows() > 0)
		{
			return TRUE;
		}
		
		return FALSE;
	}
	
	function get_batas_produk($idp)
	{
		$hariini  = date("Y-m-d");
		
		$this->db->where('idperusahaan', $idp);
		$this->db->where('tanggal', $hariini);
		
		$query = $this->db->get('dn_product');
		if($query->num_rows() < 20)
		{
			return TRUE;
		} 
		return FALSE;
	}
	
	function set_produk($data)
	{
		$this->db->insert('dn_product',$data);
		if($this->db->affected_rows() > 0)
		{
			return TRUE;
		}
		return FALSE;
	}
	
	function update_produk($data, $pid)
	{
		$this->db->where('idproduk', $pid);
		$this->db->update('dn_product', $data);
		if($this->db->affected_rows() > 0)
		{
			return TRUE;
		}
		return FALSE;
	}
	
	function del_produk($idp)
	{
		$this->db->where('idproduk',$idp);
		$this->db->delete('dn_product');
		
		if($this->db->affected_rows() > 0)
		{
			return TRUE;
		}
		
		return FALSE;
	}
	
	function get_daftar_loker($uid, $awal)
	{
		$this->db->select('j.idjob, j.posisi, j.tipe, j.lokasi, j.berakhir')->from('dn_job AS j')->join('dn_company AS c','j.idperusahaan=c.idperusahaan');
		$this->db->where('c.uid',$uid);
		$this->db->order_by('j.idjob','desc');
		$this->db->limit(20,$awal);
		
		$query = $this->db->get();
		if($query->num_rows() > 0)
		{
			return $query->result();
		}
		return FALSE;
	}
	
	
	function get_data_loker($idj, $uid)
	{
		$this->db->select('j.idjob, j.idperusahaan, j.posisi, j.deskripsi, j.cara, j.tipe, j.lokasi, j.gambar, j.berakhir')->from('dn_job AS j')->join('dn_company AS c','j.idperusahaan=c.idperusahaan');
		$this->db->where('j.idjob', $idj);
		$this->db->where('c.uid', $uid);
		
		$query = $this->db->get();	
		if($query->num_rows() > 0)
		{
			return $query->result();
		}
		return FALSE;
	}
	
	function hitung_loker($uid)
	{
		$this->db->select('dn_job.idjob')->from('dn_job')->join('dn_company','dn_job.idperusahaan=dn_company.idperusahaan')->where('dn_company.uid',$uid);
		
		$query = $this->db->get();
		return $query->num_rows();
	}
	
	function cek_pemilik_loker($idj, $uid)
	{
		$this->db->select('j.idjob');
		$this->db->from('dn_job AS j');
		$this->db->join('dn_company AS p', 'j.idperusahaan=p.idperusahaan');
		$this->db->where('j.idjob', $idj);
		$this->db->where('p.uid', $uid);
		
		$query = $this->db->get();
		if($query->num_rows() > 0)
		{
			return TRUE;
		}
		
		return FALSE;
	}
	
	function get_batas_loker($idp)
	{
		$hariini  = date("Y-m-d");
		
		$this->db->where('idperusahaan',$idp);
		$this->db->where('tanggal',$hariini);
		
		$query = $this->db->get('dn_product');
		if($query->num_rows() < 20)
		{
			return TRUE;
		} 
		return FALSE;
	}
	
	function set_loker($data)
	{
		$this->db->insert('dn_job',$data);
		if($this->db->affected_rows() > 0){
			return TRUE;
		}
		return FALSE;
	}
	
	function update_loker($data,$idj)
	{
		$this->db->where('idjob',$idj);
		$this->db->update('dn_job',$data);
		if($this->db->affected_rows() > 0){
			return TRUE;
		}
		return FALSE;
	}
	
	function del_loker($idj)
	{
		$this->db->where('idjob',$idj);
		$this->db->delete('dn_job');
		
		if($this->affected_rows() > 0)
		{
			return TRUE;
		}
		
		return FALSE;
	}
	
	/*
	 * not good coy
	function getPromosi($uid){
		$hariini = date("Y-m-d");
		
		$this->db->select('p.idpromosi,p.idperusahaan,p.judul');
		$this->db->from('dn_promote AS p');
		$this->db->join('dn_company AS c', 'p.idperusahaan = c.idperusahaan');
		$this->db->where('c.uid',$uid);
		$this->db->where('p.berakhir >= ',$hariini);
		$this->db->order_by('p.idpromosi','desc');
		$query = $this->db->get();
		
		if($query->num_rows() > 0){
			return $query->result();
		}
		return FALSE;
	}
	
	function getDataPromosi($idp,$uid){
		
		$this->db->select('p.idpromosi,p.idperusahaan,p.judul,p.promosi,p.gambar,p.berakhir');
		$this->db->from('dn_promote AS p');
		$this->db->join('dn_company AS c', 'p.idperusahaan = c.idperusahaan');
		
		$this->db->where('p.idpromosi',$idp);
		$this->db->where('c.uid',$uid);
		
		$query = $this->db->get();	
		if($query->num_rows() > 0){
			return $query->result();
		}
		return FALSE;
	}
	
	function getBatasPromosi($uid){
		$hariini = date("Y-m-d");
		
		$this->db->select('dn_promote.idpromosi');
		$this->db->from('dn_promote');
		$this->db->join('dn_company','dn_company.idperusahaan=dn_promote.idperusahaan');
		$this->db->where('dn_company.uid',$uid);
		$this->db->where('dn_promote.berakhir >=',$hariini);
		
		$query = $this->db->get();
		if($query->num_rows() < 10){
			return TRUE;
		} 
		return FALSE;
	}
	
	function setPromosi($data)
	{
		$this->db->insert('dn_promote',$data);
		if($this->db->affected_rows() > 0){
			return TRUE;
		}
		return FALSE;
	}
	
	function updatePromosi($data,$idp)
	{
		$this->db->where('idpromosi',$idp);
		$this->db->update('dn_promote',$data);
		if($this->db->affected_rows() > 0)
		{
			return TRUE;
		}
		return FALSE;
	}
	* 
	*/
	
    function get_daftar_acara($uid)
    {
		$hariini = date("Y-m-d");
		
		$this->db->select('idacara,nama');
		$this->db->where('uid',$uid);
		$this->db->where('tanggal >= ',$hariini);
		$this->db->order_by('idacara','desc');
		$query = $this->db->get('dn_event');
		
		if($query->num_rows() > 0)
		{
			return $query->result();
		}
		return FALSE;
	}
	
	function get_data_acara($ida,$uid)
	{
		
		$this->db->where('idacara', $ida);
		$this->db->where('uid',$uid);
		
		$query = $this->db->get('dn_event');	
		if($query->num_rows() > 0)
		{
			return $query->result();
		}
		return FALSE;
	}
	
	function get_batas_acara($uid)
	{
		$hariini = date("Y-m-d");
		$this->db->where('uid',$uid);
		$this->db->where('tanggal >=',$hariini);
		
		$query = $this->db->get('dn_event');
		if($query->num_rows() < 10)
		{
			return TRUE;
		} 
		return FALSE;
	}
	
	function set_acara($data)
	{
		$this->db->insert('dn_event',$data);
		if($this->db->affected_rows() > 0)
		{
			return TRUE;
		}
		return FALSE;
	}
	
	function update_acara($data,$ida,$uid)
	{
		$this->db->where('idacara',$ida);
		$this->db->where('uid',$uid);
		$this->db->update('dn_event',$data);
		if($this->db->affected_rows() > 0)
		{
			return TRUE;
		}
		return FALSE;
	}
	
	function del_acara($ida, $uid)
	{
		$this->db->where('idacara', $ida);
		$this->db->where('uid', $uid);
		$this->db->delete('dn_event');
		
		if($this->affected_rows() > 0)
		{
			return TRUE;
		}
		
		return FALSE;
	}
	
	function get_data_user($uid)
	{
		$this->db->where('uid', $uid);
		$this->db->where('level', 2);
		$user = $this->db->get('dn_users');
		if($user->num_rows() > 0)
		{
			return $user->result();
		}
		
		return FALSE;
	}
	
	function cek_user_password($uid, $password)
	{
		$this->db->where('uid', $uid);
		$this->db->where('password', $password);
		$cek = $this->db->get('dn_users');
		if($cek->num_rows() > 0)
		{
			return TRUE;
		}
		
		return FALSE;
	}
	
	function update_user($data, $uid)
	{
		$this->db->where('uid',$uid);
		$this->db->update('dn_users',$data);
		if($this->db->affected_rows() > 0)
		{
			return TRUE;
		}
		return FALSE;
	}
	
	function get_social_network($uid=0)
	{
		$this->db->where('uid', $uid);
		$hasil = $this->db->get('dn_social');
		
		if($hasil->num_rows() > 0)
		{
			return $hasil->result();
		}
		
		return FALSE;
	}
	
	function cek_social_network($uid=0, $sc)
	{
		$this->db->where('uid', $uid);
		$this->db->where('app', $sc);
		$hasil = $this->db->get('dn_social');
		
		if($hasil->num_rows() > 0)
		{
			return TRUE;
		}
		
		return FALSE;
	}
	
	function cek_social_network_id($sc, $sid)
	{
		
		$this->db->where('app', $sc);
		$this->db->where('sid', $sid);
		$hasil = $this->db->get('dn_social');
		
		if($hasil->num_rows() > 0)
		{
			return TRUE;
		}
		
		return FALSE;
	}
	
	function set_social_network($uid=0, $sc, $sid)
	{
		$data = array('uid' => $uid, 'app' => $sc, 'sid' => $sid);
		$this->db->insert('dn_social', $data);
		
		if($this->db->affected_rows() > 0)
		{
			return TRUE;
		}
		
		return FALSE;
	}
	
	function update_social_network($uid, $sc, $sid)
	{
		$data = array('sid' => $sid);
		
		$this->db->where('uid', $uid);
		$this->db->where('app', $sc);
		$this->db->update('dn_social', $data);
		
		if($this->db->affected_rows() > 0)
		{
			return TRUE;
		}
		
		return FALSE;
	} 
    
}

/* End of file partner_model.php */
/* Location: ./application/models/partner_model.php */
